package com.wang.designpattern.responsibilitychain;

import com.wang.designpattern.responsibilitychain.domain.AttendanceClockDO;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Objects;

/**
 * 旷工处理类
 */
public class PmAbsentAttendanceHandler extends AttendanceHandler {
    public PmAbsentAttendanceHandler(String name) {
        super(name);
    }

    @Override
    public void processRequest(Map<String, AttendanceClockDO> clockResultDataMap, AttendanceClockDO attendanceClockDO) {
        AttendanceClockDO clockResultData = clockResultDataMap.get(AttendanceConstant.ExceptionType.ABSENT.getExceptionType());
        if (Objects.isNull(clockResultData)) {
            clockResultData = new AttendanceClockDO();
            BeanUtils.copyProperties(attendanceClockDO, clockResultData);
        }
        //1、“下午实际上班打卡时间”为空
        if (StringUtils.isEmpty(attendanceClockDO.getPmStartTime())) {
            clockResultData.setExceptionType(AttendanceConstant.ExceptionType.ABSENT.getExceptionType());
            clockResultData.setExceptionCount(clockResultData.getExceptionCount()+0.5f);
            clockResultData.setExceptionUnit("D");
            clockResultDataMap.put(AttendanceConstant.ExceptionType.ABSENT.getExceptionType(), clockResultData);
            return;
        }
        //2、“下午实际下班打卡时间”为空
        if (StringUtils.isEmpty(attendanceClockDO.getPmEndTime())) {
            clockResultData.setExceptionType(AttendanceConstant.ExceptionType.ABSENT.getExceptionType());
            clockResultData.setExceptionCount(clockResultData.getExceptionCount()+0.5f);
            clockResultData.setExceptionUnit("D");
            clockResultDataMap.put(AttendanceConstant.ExceptionType.ABSENT.getExceptionType(), clockResultData);
            return;
        }
        //3、“下午实际上班打卡时间” - “下午上班时间” > 30分钟
        if (DateUtils.calcTime(attendanceClockDO.getClockDate(), attendanceClockDO.getPmStartTime(),
                attendanceClockDO.getPmSetStartTime()) > 30) {
            clockResultData.setExceptionType(AttendanceConstant.ExceptionType.ABSENT.getExceptionType());
            clockResultData.setExceptionCount(clockResultData.getExceptionCount()+0.5f);
            clockResultData.setExceptionUnit("D");
            clockResultDataMap.put(AttendanceConstant.ExceptionType.ABSENT.getExceptionType(), clockResultData);
            return;
        }
        //4、“下午下班时间” - “下午实际下班打卡时间” > 30分钟
        if (DateUtils.calcTime(attendanceClockDO.getClockDate(), attendanceClockDO.getPmSetEndTime(),
                attendanceClockDO.getPmEndTime()) > 30) {
            clockResultData.setExceptionType(AttendanceConstant.ExceptionType.ABSENT.getExceptionType());
            clockResultData.setExceptionCount(clockResultData.getExceptionCount()+0.5f);
            clockResultData.setExceptionUnit("D");
            clockResultDataMap.put(AttendanceConstant.ExceptionType.ABSENT.getExceptionType(), clockResultData);
            return;
        }
        handler.processRequest(clockResultDataMap, attendanceClockDO);
    }
}
